home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / src / ode-initval / rk4.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-18  |  4.5 KB  |  208 lines

  1. /* ode-initval/rk4.c
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. /* Runge-Kutta 4, Classical */
  21.  
  22. /* Author:  G. Jungman
  23.  */
  24. #include <config.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <gsl/gsl_errno.h>
  28. #include <gsl/gsl_odeiv.h>
  29.  
  30. #include "odeiv_util.h"
  31.  
  32. typedef struct
  33. {
  34.   double *k;
  35.   double *y0;
  36.   double *ytmp;
  37. }
  38. rk4_state_t;
  39.  
  40. static void *
  41. rk4_alloc (size_t dim)
  42. {
  43.   rk4_state_t *state = (rk4_state_t *) malloc (sizeof (rk4_state_t));
  44.  
  45.   if (state == 0)
  46.     {
  47.       GSL_ERROR_NULL ("failed to allocate space for rk4_state", GSL_ENOMEM);
  48.     }
  49.  
  50.   state->k = (double *) malloc (dim * sizeof (double));
  51.  
  52.   if (state->k == 0)
  53.     {
  54.       free (state);
  55.       GSL_ERROR_NULL ("failed to allocate space for k", GSL_ENOMEM);
  56.     }
  57.  
  58.   state->y0 = (double *) malloc (dim * sizeof (double));
  59.  
  60.   if (state->y0 == 0)
  61.     {
  62.       free (state->k);
  63.       free (state);
  64.       GSL_ERROR_NULL ("failed to allocate space for y0", GSL_ENOMEM);
  65.     }
  66.  
  67.   state->ytmp = (double *) malloc (dim * sizeof (double));
  68.  
  69.   if (state->ytmp == 0)
  70.     {
  71.       free (state->y0);
  72.       free (state->k);
  73.       free (state);
  74.       GSL_ERROR_NULL ("failed to allocate space for ytmp", GSL_ENOMEM);
  75.     }
  76.  
  77.   return state;
  78. }
  79.  
  80.  
  81. static int
  82. rk4_apply (void *vstate,
  83.        size_t dim,
  84.        double t,
  85.        double h,
  86.        double y[],
  87.        double yerr[],
  88.        const double dydt_in[],
  89.        double dydt_out[], 
  90.            const gsl_odeiv_system * sys)
  91. {
  92.   rk4_state_t *state = (rk4_state_t *) vstate;
  93.  
  94.   size_t i;
  95.   int status = 0;
  96.  
  97.   double *const k = state->k;
  98.   double *const y0 = state->y0;
  99.   double *const ytmp = state->ytmp;
  100.  
  101.  
  102.   /* Copy the starting value. We will write over
  103.    * the y[] vector, using it for scratch and
  104.    * then filling it with the final result.
  105.    */
  106.  
  107.   DBL_MEMCPY (y0, y, dim);
  108.  
  109.   if (dydt_in != NULL)
  110.     {
  111.       DBL_MEMCPY (k, dydt_in, dim);
  112.     }
  113.   else
  114.     {
  115.       int s = GSL_ODEIV_FN_EVAL (sys, t, y0, k);
  116.       GSL_STATUS_UPDATE (&status, s);
  117.     }
  118.  
  119.   for (i = 0; i < dim; i++)
  120.     {
  121.       y[i] = h / 6.0 * k[i];    /* use y[] to store delta_y */
  122.       ytmp[i] = y0[i] + 0.5 * h * k[i];
  123.     }
  124.  
  125.   /* k2 step */
  126.   {
  127.     int s = GSL_ODEIV_FN_EVAL (sys, t + 0.5 * h, ytmp, k);
  128.     GSL_STATUS_UPDATE (&status, s);
  129.   }
  130.  
  131.   for (i = 0; i < dim; i++)
  132.     {
  133.       y[i] += h / 3.0 * k[i];
  134.       ytmp[i] = y0[i] + 0.5 * h * k[i];
  135.     }
  136.  
  137.   /* k3 step */
  138.   {
  139.     int s = GSL_ODEIV_FN_EVAL (sys, t + 0.5 * h, ytmp, k);
  140.     GSL_STATUS_UPDATE (&status, s);
  141.   }
  142.  
  143.   for (i = 0; i < dim; i++)
  144.     {
  145.       y[i] += h / 3.0 * k[i];
  146.       ytmp[i] = y0[i] + h * k[i];
  147.     }
  148.  
  149.   /* k4 step, error estimate, and final sum */
  150.   {
  151.     int s = GSL_ODEIV_FN_EVAL (sys, t + h, ytmp, k);
  152.     GSL_STATUS_UPDATE (&status, s);
  153.   }
  154.  
  155.   for (i = 0; i < dim; i++)
  156.     {
  157.       y[i] += h / 6.0 * k[i];
  158.       yerr[i] = h * y[i];
  159.       y[i] += y0[i];
  160.       if (dydt_out != NULL)
  161.     dydt_out[i] = k[i];
  162.     }
  163.  
  164.   return status;
  165. }
  166.  
  167. static int
  168. rk4_reset (void *vstate, size_t dim)
  169. {
  170.   rk4_state_t *state = (rk4_state_t *) vstate;
  171.  
  172.   DBL_ZERO_MEMSET (state->k, dim);
  173.   DBL_ZERO_MEMSET (state->y0, dim);
  174.   DBL_ZERO_MEMSET (state->ytmp, dim);
  175.  
  176.   return GSL_SUCCESS;
  177. }
  178.  
  179. static unsigned int
  180. rk4_order (void *vstate)
  181. {
  182.   rk4_state_t *state = (rk4_state_t *) vstate;
  183.   state = 0; /* prevent warnings about unused parameters */
  184.   return 4;
  185. }
  186.  
  187. static void
  188. rk4_free (void *vstate)
  189. {
  190.   rk4_state_t *state = (rk4_state_t *) vstate;
  191.   free (state->k);
  192.   free (state->y0);
  193.   free (state->ytmp);
  194.   free (state);
  195. }
  196.  
  197. static const gsl_odeiv_step_type rk4_type = { "rk4",    /* name */
  198.   1,                /* can use dydt_in */
  199.   0,                /* gives exact dydt_out */
  200.   &rk4_alloc,
  201.   &rk4_apply,
  202.   &rk4_reset,
  203.   &rk4_order,
  204.   &rk4_free
  205. };
  206.  
  207. const gsl_odeiv_step_type *gsl_odeiv_step_rk4 = &rk4_type;
  208.